package kz.kstu.scantest.business.server.util.transaction;

import java.util.Properties;
import java.util.Set;

import javax.transaction.TransactionManager;

import org.apache.geronimo.gbean.AbstractName;
import org.apache.geronimo.gbean.AbstractNameQuery;
import org.apache.geronimo.kernel.Kernel;
import org.apache.geronimo.kernel.KernelRegistry;
import org.hibernate.HibernateException;
import org.hibernate.transaction.TransactionManagerLookup;

public class GeronimoTransactionManagerLookup implements TransactionManagerLookup {

    public static final String UserTransactionName = "java:comp/UserTransaction";

    public TransactionManager getTransactionManager(Properties props) throws HibernateException {
        try {
            Kernel kernel = KernelRegistry.getSingleKernel();
            AbstractNameQuery query = new AbstractNameQuery(TransactionManager.class.getName());
            Set<AbstractName> names = kernel.listGBeans(query);
            if (names.size() != 1) {
                throw new IllegalStateException("Expected one transaction manager, not " + names.size());
            }
            AbstractName name = names.iterator().next();
            return (TransactionManager) kernel.getGBean(name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new HibernateException("Geronimo Transaction Manager Lookup Failed", e);
        }
    }

    public String getUserTransactionName() {
        return UserTransactionName;
    }
}